//
//  UserAccount.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/11/1.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "UserAccount.h"
#import "AccountViewModel.h"

@implementation UserAccount


//-(id)copyWithZone:(NSZone *)zone {
//    
//    AccountViewModel *newClass = [[AccountViewModel alloc]init];
//    newClass.access_token = self.access_token;
//    return newClass;
//}



-(void)setExpires_in:(NSTimeInterval)expires_in{
    
    _expires_in = expires_in;
    
//    NSTimeInterval interval = [expires_in timeIntervalSinceNow];
    
    //时间戳转时间
    
    self.expires_date = [NSDate dateWithTimeIntervalSinceNow:expires_in];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict;{
    
    self = [super init];
    if (self) {
        
        [self setValuesForKeysWithDictionary:dict];
        
    }
    return self;
    
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key;{}

-(NSString *)description;{
    
    //    "access_token","expires_in","uid","name","avatar_large"
    
    return  [NSString stringWithFormat:@"access_token = %@,expires_in = %f,expires_date = %@,uid = %@,name = %@,avatar_large = %@",self.access_token,self.expires_in,self.expires_date,self.uid,self.name,self.avatar_large];
    
}


//实现代理协议NScoding方法
//将自定义对象转换为二进制数据
- (void)encodeWithCoder:(NSCoder *)aCoder;{
    
    [aCoder encodeObject:_access_token forKey:@"access_token"];
    
    [aCoder encodeObject:@"_expires_in" forKey:@"expires_in"];
    
    [aCoder encodeObject:_expires_date forKey:@"expires_date"];
    
    [aCoder encodeObject:_uid forKey:@"uid"];
    
    [aCoder encodeObject:_name forKey:@"name"];
    
    [aCoder encodeObject:_avatar_large forKey:@"avatar_large"];

}

//将二进制数据转换为自定义对象
- (instancetype)initWithCoder:(NSCoder *)aDecoder;{
    
    self = [super init];
    
    if (self) {
        
        self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
        
        self.expires_in = [[aDecoder decodeObjectForKey:@"expires_in"] doubleValue];
        
        self.expires_date = [aDecoder decodeObjectForKey:@"expires_date"];
        
        self.uid = [aDecoder decodeObjectForKey:@"uid"];
        
        self.name = [aDecoder decodeObjectForKey:@"name"];
        
        self.avatar_large = [aDecoder decodeObjectForKey:@"avatar_large"];
    }
    
    return self;
    
}


//向沙盒中储存数据
- (void)saveAccount;{
    
//    stringByAppendingString表示在当前路径最后一层下新建一个文件,而stringByAppendingPathComponent表示在当前路径的下一层再创建一个文件
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) objectAtIndex:0] stringByAppendingPathComponent:@"useraccount.plist"];
    
    NSLog(@"%@",path);
    
    [NSKeyedArchiver archiveRootObject:self toFile:path];
    
}


//读取数据
+ (UserAccount *)loadUserAccount;{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"useraccount.plist"];

  UserAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    NSLog(@"%@",account.access_token);
    
//  获取系统的当前时间
    NSDate *date = [NSDate date]; // 获得时间对象
    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *nowDate = [date dateByAddingTimeInterval:time];
    NSLog(@"expires_date = %@,nowDate = %@",account.expires_date,nowDate);
    
    NSComparisonResult result= [account.expires_date compare:nowDate];
    NSLog(@"%ld",(long)result);
    
    
    if ( (long)[account.expires_date compare:nowDate] == NSOrderedDescending)
    {
        return account;
    }
    return nil;
}

@end
